/*
 * Copyright (C) 2012-2020 Markus Junginger, greenrobot (http://greenrobot.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.greenrobot.eventbus;

import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import org.greenrobot.eventbus.ohos.OhosLogger;

import java.util.logging.Level;

public interface Logger {
    HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, "[Logger] ");

    void log(Level level, String msg);

    void log(Level level, String msg, Throwable th);

    class JavaLogger implements Logger {
        protected final java.util.logging.Logger logger;

        public JavaLogger(String tag) {
            logger = java.util.logging.Logger.getLogger(tag);
        }

        @Override
        public void log(Level level, String msg) {
            // TODO Replace logged method with caller method
            logger.log(level, msg);
        }

        @Override
        public void log(Level level, String msg, Throwable th) {
            // TODO Replace logged method with caller method
            logger.log(level, msg, th);
        }

    }

    class SystemOutLogger implements Logger {

        @Override
        public void log(Level level, String msg) {
            HiLog.error(LABEL_LOG, "[" + level + "] " + msg);
        }

        @Override
        public void log(Level level, String msg, Throwable th) {
            HiLog.error(LABEL_LOG, "[" + level + "] " + msg);
            th.printStackTrace(System.out);
        }

    }

    class Default {
        public static Logger get() {
            // also check main looper to see if we have "good" openhormony classes (not Stubs etc.)
            return OhosLogger.isOhosLogAvailable() && EventBus.getUiTaskDispatcher() != null
                    ? new OhosLogger("EventBus") :
                    new Logger.SystemOutLogger();
        }
    }

}
